package fr.utbm.VI51.lemmings_world_editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 * @author Adrien
 * 
 * It is a dialog window which permits to configure the settings of a map
 */
public class MapConfigDialog extends JDialog{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 579473090422373570L;
	private MapConfig mapConfig;
	JTextField heightField;
	JTextField widthField;
	JTextField pixelsPerCaseField;
	
	private JLabel casesLabel;
	private JLabel casesLabel2;
	private JLabel pixelsPerCaseLabel;
	
	/**
	 * Constructor of the map config dialog
	 * 
	 * @param parent the parent of this dialog window
	 * @param title the title of the dialog window
	 * @param modal <tt>true</tt> if the dialog window is modal
	 */
	public MapConfigDialog(JFrame parent, String title, boolean modal){
		
		super(parent, title, modal);
		this.setSize(300, 300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		
		// Height textField:
		JPanel heightPanel = new JPanel();
		heightPanel.setPreferredSize(new Dimension(250, 70));
		heightPanel.setBorder(BorderFactory.createTitledBorder("Map height")); //$NON-NLS-1$
		this.casesLabel = new JLabel(" cases"); //$NON-NLS-1$
		this.heightField = new JTextField("96"); //$NON-NLS-1$
		this.heightField.setPreferredSize(new Dimension(90, 30));
		heightPanel.add(this.heightField);
		heightPanel.add(this.casesLabel);
		
		// Width textField
		JPanel widthPanel = new JPanel();
		widthPanel.setPreferredSize(new Dimension(250, 70));
		widthPanel.setBorder(BorderFactory.createTitledBorder("Map width")); //$NON-NLS-1$
		this.casesLabel2 = new JLabel(" cases"); //$NON-NLS-1$
		this.widthField = new JTextField("128"); //$NON-NLS-1$
		this.widthField.setPreferredSize(new Dimension(90, 30));
		widthPanel.add(this.widthField);
		widthPanel.add(this.casesLabel2);
		
		// Number of pixels per cases textField
		JPanel pixelPerCasesPanel = new JPanel();
		pixelPerCasesPanel.setPreferredSize(new Dimension(250, 70));
		pixelPerCasesPanel.setBorder(BorderFactory.createTitledBorder("Number of pixels per cases")); //$NON-NLS-1$
		this.pixelsPerCaseLabel = new JLabel(" pixels per case"); //$NON-NLS-1$
		this.pixelsPerCaseField = new JTextField("8"); //$NON-NLS-1$
		this.pixelsPerCaseField.setPreferredSize(new Dimension(90, 30));
		pixelPerCasesPanel.add(this.pixelsPerCaseField);
		pixelPerCasesPanel.add(this.pixelsPerCaseLabel);
		
		JPanel content = new JPanel();
		content.add(heightPanel);
		content.add(widthPanel);
		content.add(pixelPerCasesPanel);
		
		JPanel control = new JPanel();
		JButton okButton = new JButton("OK"); //$NON-NLS-1$
		
		
		
		okButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {				
				MapConfigDialog.this.setMapConfig(new MapConfig(Integer.parseInt(getHeight()), Integer.parseInt(getWidth()), Integer.parseInt(getpixelsPerCases()))); 
				setVisible(false);
			}
			
			public String getHeight(){
				return (MapConfigDialog.this.heightField.getText().equals("")) ? "96" : MapConfigDialog.this.heightField.getText(); //$NON-NLS-1$ //$NON-NLS-2$
			}
			
			public String getWidth(){
				return (MapConfigDialog.this.widthField.getText().equals("")) ? "128" : MapConfigDialog.this.widthField.getText(); //$NON-NLS-1$ //$NON-NLS-2$
			}
			
			public String getpixelsPerCases(){
				return (MapConfigDialog.this.pixelsPerCaseField.getText().equals("")) ? "8" : MapConfigDialog.this.pixelsPerCaseField.getText(); //$NON-NLS-1$ //$NON-NLS-2$
			}
			
		});

		control.add(okButton);
		
		this.getContentPane().add(content, BorderLayout.CENTER);
		this.getContentPane().add(okButton, BorderLayout.SOUTH);
		
		this.setVisible(true);
		
	}

	/**
	 * Set the map Config
	 * 
	 * @param mapConfig the mapConfig
	 */
	public void setMapConfig(MapConfig mapConfig) {
		this.mapConfig = mapConfig;
	}

	/**
	 * Returns the map config
	 * 
	 * @return the mapConfig
	 */
	public MapConfig getMapConfig() {
		return this.mapConfig;
	}

}
